﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class Goepe : Information
    {
        string name = "";

        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("usernamego1", name);
            pd.Add("passwordgo1", pass);
            pd.Add("Submit2", "");
            string html = source.Post("http://www.goepe.com/member/index.php?id=", pd.Read(), "http://www.goepe.com/member/login.php");
            if (html.IndexOf("target=\"_top\">退出</a") != -1)
            {
                this.name = name;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            string text = MyFunction.ToText(content);
            string html = source.Get("http://www.goepe.com/member/producttwo.php?prodsortid=" + site.TypeId[0]);
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("hiddenid", "");
            sfd.Write("prodml", site.TypeId[0]);
            sfd.Write("product_name", title);
            sfd.Write("userid", name);
            sfd.Write("sid", Regex.Match(html, "parent2= new Array\\(\"([0-9]+)").Groups[1].Value);
            sfd.Write("productmode", pif.Get("产品名称"));
            sfd.Write("brand", pif.Get("产品名称"));
            sfd.Write("danwei", "台");
            sfd.Write("price", pif.Get("价格"));
            sfd.Write("imageid1", UpImage());
            sfd.Write("descriptions", text.Substring(0, 100));
            sfd.Write("content", content);
            sfd.Write("Submit99", "点击发布供应产品");
            //结尾
            html = source.Post("http://www.goepe.com/member/product_insertnew.php", sfd.GetContentType(), sfd.Read(), "http://www.goepe.com/member/producttwo.php?prodsortid=" + site.TypeId[0] + "&imgid38=");
            html = source.Get("http://www.goepe.com/member/editproduct.php?show=table2");
            if (Regex.IsMatch(html, ">" + title + "</a>"))
            {
                result.Code = 1;
                result.Content = "http://www.goepe.com/apollo/offer-" + name + "-" + Regex.Match(html, "=([0-9]+)\">" + title + "</a>").Groups[1].Value + ".html";
            }
            return result;
        }
        public string UpImage()
        {
            string html = "";
            SourceFormData sfd = new SourceFormData("gb2312");
            //发送文件
            sfd.WriteFile("fileToUpload", RandomPicture.Get());
            //结尾
            html = source.Post("http://www.goepe.com/member/ajax_fileupload.php?watertype=water", sfd.GetContentType(), sfd.Read(), "http://www.goepe.com/member/producttwo.php?prodsortid=8810&imgid38=");
            return Regex.Match(html, "imgid: '([0-9]+)'").Groups[1].Value;
        }
    }
}
